﻿using System;
using System.Linq;
using System.Web.Mvc;
using Icms.Core.Model;
using Icms.Core.Repository;
using Icms.Core.Service;
using Icms.WebUI.Builder;
using Icms.WebUI.Dto;
using Omu.Awesome.Mvc;

namespace Icms.WebUI.Controllers
{
    public class ParticipantController : Crudere<Participant, ParticipantCreateInput, ParticipantEditInput>
    {
        private readonly IRepo<Party> partyRepo;
        private readonly IRepo<Case> caseRepo;

        public ParticipantController(ICrudService<Participant> s, IParticipantMapper<Participant, ParticipantCreateInput> v, IParticipantMapper<Participant, ParticipantEditInput> ve, IRepo<Party> partyRepo, IRepo<Case> caseRepo)
            : base(s, v, ve)
        {
            this.partyRepo = partyRepo;
            this.caseRepo = caseRepo;
        }

        public override ActionResult Index()
        {
            ViewBag.Case = caseRepo.Get(new Guid(Request["CaseId"]));
            return View();
        }

        public virtual ActionResult Search(string title, Guid CaseId, int page = 1, int ps = 5)
        {
            var src = partyRepo.Where(o => o.CaseId == CaseId);
            var rows = this.RenderView("rows", src.Skip((page - 1) * ps).Take(ps));

            return Json(new { rows, more = src.Count() > page * ps });
        }

        public override ActionResult Row(Guid id)
        {
            return View("rows", new[] { partyRepo.Get(id) });
        }
    }
}